#!/bin/bash
# common functions used in multiple scripts

get_os_release() {
        local os_release result

        os_release=`grep -i '^name=' /etc/os-release | awk -F \" '{print $2}' | tr "[:upper:]" "[:lower:]"`

        if [ "X${os_release:0:6}" = "Xubuntu" ]; then
                result="ubuntu"
        elif [ "X${os_release:0:6}" = "Xcentos" ]; then
                result="centos"
        elif [ "X${os_release:0:6}" = "Xanolis" ]; then
                result="anolis"
        elif [ "X${os_release:0:5}" = "Xkylin" ]; then
                result="kylin"
        else
                echo "Unknown OS release"
                exit 1
        fi

        echo $result
}

get_os_info() {
    local PKG_SUFFIX=""
    local OS_TYPE=""
    local OS_VERSION_ID="unknown"

    if [ -f "/etc/os-release" ]; then
        source /etc/os-release
        dpkg_output=$(dpkg -l 2>/dev/null | grep -v '^Desired=')
        if [ -n "$dpkg_output" ]; then
            PKG_SUFFIX="deb"
        else
            rpm_output=$(rpm -qa 2>/dev/null)
            if [ -n "$rpm_output" ]; then
                PKG_SUFFIX="rpm"
            else
                PKG_SUFFIX="unknown"
                echo "don't have dpkg/rpm package manage"
                exit 1
            fi
        fi
        OS_TYPE=$ID
        OS_VERSION_ID=${VERSION_ID:-"unknown"}
    else
        echo "/etc/os-release not found"
        exit 1
    fi

    echo "$PKG_SUFFIX"
    echo "$OS_TYPE"
    echo "$OS_VERSION_ID"
}